﻿package {
	import flash.display.Sprite;

	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	import org.papervision3d.materials.MovieAssetMaterial;	


	/**
	 * @author Niels
	 */
	public class Main extends Sprite {
		private var viewport:Viewport3D;
		private var renderer:BasicRenderEngine;
		private var scene:Scene3D;
		private var camera:Camera3D;
		public function Main() {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			this.viewport = new Viewport3D(0,0,true,true);
			this.addChild(this.viewport);
			this.renderer = new BasicRenderEngine();
			
			this.scene = new Scene3D();
			this.camera =  new Camera3D();
			this.camera.zoom = 15;
			
			for (var i : Number = 0; i < 10; i++) {
				addAvatar();
			}
			
			addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			
		}
		
		private function handleEnterFrame(event:Event):void {
			//plane.yaw(0.5);
			this.camera.hover(1, (200-mouseX)*.001, (200-mouseY)*.001);
			renderer.renderScene(scene, camera, viewport);
		}
		
		private function addAvatar():void {
			var avatar:Avatar = new Avatar();
			avatar.blendMode = "erase";
			trace(avatar);
			this.addChild(avatar);
			
			//var material:MovieAssetMaterial = new MovieAssetMaterial("Avatar",true);
			var material:MovieMaterial = new MovieMaterial(avatar, true, true);
			material.interactive = true;
			material.oneSide = false;
			material.smooth = true;
			
			var plane:Plane = new Plane(material, 120, 180, 1, 1);
			plane.x = Math.random()*500-250;
			plane.z = Math.random()*500-250;
			this.scene.addChild(plane);
		}
	}
}
